import 'package:flutter/material.dart';

class AnimateDemo extends StatelessWidget {
  const AnimateDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimateDemo'),
        elevation: 0.0,
      ),
      body: AnimateHomeDemo(),
    );
  }
}

class AnimateHomeDemo extends StatefulWidget {
  AnimateHomeDemo({Key key}) : super(key: key);

  @override
  _AnimateHomeDemoState createState() => _AnimateHomeDemoState();
}

class _AnimateHomeDemoState extends State<AnimateHomeDemo> with TickerProviderStateMixin {
  AnimationController animationController;
  Animation animation;
  Animation animationColor;
  CurvedAnimation curve;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    animationController = AnimationController(
      // value: 32.0,
      // lowerBound: 32.0,
      // upperBound: 100.0,
      duration: Duration(milliseconds: 3000),
      vsync: this
    );

    curve = CurvedAnimation(
      parent: animationController,
      curve: Curves.bounceIn
    );
    animation = Tween(begin: 32.0, end: 100.0).animate(curve);
    animationColor = ColorTween(begin: Colors.red, end: Colors.red[900]).animate(curve);
    animationController.addListener(() {
      // setState(() {
        
      // });
    });

    animationController.addStatusListener((status) {
      print('status is $status');
    });

  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    
    animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // return Center(
    //   child: IconButton(
    //     icon: Icon(Icons.favorite),
    //     iconSize: animation.value,
    //     color: animationColor.value,
    //     onPressed: (){
    //       if (animationController.status == AnimationStatus.completed) {
    //         animationController.reverse();
    //       } else {
    //         animationController.forward();
    //       }
    //     }
    //   )
    // );
    return AnimationHeart(
      animations: [animation, animationColor],
      controller: animationController,
    );
  }
}

class AnimationHeart extends AnimatedWidget {
  final List animations;
  final AnimationController controller;

  AnimationHeart({
    this.animations,
    this.controller
  }): super(listenable: controller);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: IconButton(
        icon: Icon(Icons.favorite),
        iconSize: animations[0].value,
        color: animations[1].value,
        onPressed: (){
          if (controller.status == AnimationStatus.completed) {
            controller.reverse();
          } else {
            controller.forward();
          }
        }
      )
    );
  }

}